* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file generates the distribution of firm product scope for the domestic and export markets for Appendix Figure A.2.

* Acquirers
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_all_largestT_1865.dta", clear
keep cvrnrA aar
duplicates drop
expand 7
bysort cvrnrA aar: gen d_yr = _n - 4
gen aarA = aar + d_yr
gen p = (d_yr>0)

merge m:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_vars_dom_all_cvrnr_dro.dta", keep(match master) keepusing(nprod vrdA) nogen
rename vrdA vrdA_dom
rename nprod nprod_dom

merge m:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_cvrnr.dta", keep(match master) keepusing(nprod nmark vrdA) nogen	
rename vrdA vrdA_exp
rename nprod nprod_exp
rename nmark nmark_exp

rename cvrnrA cvrnr
rename aar aar_ori
rename aarA aar
merge m:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_firm_subset1865.dta", keep(match) keepusing(employee_cvrnr) nogen
rename cvrnr cvrnrA
rename aar aarA
rename aar_ori aar

*Replace missing with zeros
gen nprod_exp0 = nprod_exp
replace nprod_exp0 = 0 if nprod_exp==.

*In year of merger
gen nprod_dom_aar = nprod_dom if aar==aarA
gen nprod_exp_aar = nprod_exp if aar==aarA

collapse (mean) nprod_dom nprod_exp nprod_exp0 nmark_exp vrdA_dom vrdA_exp employee_cvrnr nprod_dom_aar nprod_exp_aar, by(cvrnrA aar p)

tempfile tempacquirer
save `tempacquirer.dta', replace

***

* Targets
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_all_largestT_1865.dta", clear
keep cvrnrT aar
duplicates drop
expand 4
bysort cvrnrT aar: gen d_yr = _n - 4
gen aarA = aar + d_yr
*gen p = (d_yr>0)

rename cvrnrT cvrnrA
merge m:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_vars_dom_all_cvrnr_dro.dta", keep(match master) keepusing(nprod vrdA) nogen
rename vrdA vrdT_dom
rename nprod nprod_dom

merge m:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_cvrnr.dta", keep(match master) keepusing(nprod nmark vrdA) nogen	
rename vrdA vrdT_exp
rename nprod nprod_exp
rename nmark nmark_exp

rename cvrnrA cvrnr
rename aar aar_ori
rename aarA aar
merge m:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_firm_subset1865.dta", keep(match) keepusing(employee_cvrnr) nogen
rename cvrnr cvrnrT
rename aar aarA
rename aar_ori aar

*Replace missing with zeros
gen nprod_exp0 = nprod_exp
replace nprod_exp0 = 0 if nprod_exp==.

*In year of merger
gen nprod_dom_aar = nprod_dom if aar==aarA
gen nprod_exp_aar = nprod_exp if aar==aarA

collapse (mean) nprod_dom nprod_exp nprod_exp0 nmark_exp vrdT_dom vrdT_exp employee_cvrnr nprod_dom_aar nprod_exp_aar, by(cvrnrT aar)

tempfile temptarget
save `temptarget.dta', replace

***
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_vars_dom_all_cvrnr_dro.dta", clear
rename cvrnrA cvrnr
rename aarA aar
merge 1:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\cvrnr_industry_jc.dta", keep(match) nogen
gen dig2 = floor(CVR_BRANCHE_DB07/10000)
collapse (mean) nprod_dro, by(dig2 aar)
tempfile tempnprod_dom
save `tempnprod_dom.dta', replace

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_cvrnr.dta", clear
rename cvrnrA cvrnr
rename aarA aar
merge 1:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\cvrnr_industry_jc.dta", keep(match) nogen
gen dig2 = floor(CVR_BRANCHE_DB07/10000)
collapse (mean) nprod, by(dig2 aar)
tempfile tempnprod_exp
save `tempnprod_exp.dta', replace

*Kernel density for number of products
use `tempacquirer.dta', clear
append using `temptarget.dta'
rename cvrnrA cvrnr 
merge m:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\cvrnr_industry_jc.dta", keep(match master) nogen
gen dig2A = floor(CVR_BRANCHE_DB07/10000)
drop CVR_BRANCHE_DB07
rename cvrnr cvrnrA
rename cvrnrT cvrnr 
merge m:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\cvrnr_industry_jc.dta", keep(match master) nogen
gen dig2 = floor(CVR_BRANCHE_DB07/10000)
replace dig2 = dig2A if cvrnrA!=""
rename cvrnr cvrnrT

merge m:1 dig2 aar using `tempnprod_dom.dta', keep(match master) nogen
merge m:1 dig2 aar using `tempnprod_exp.dta', keep(match master) nogen

gen lnprod_dom = ln(nprod_dom)
gen lnprod_exp = ln(nprod_exp)

gen lnprod_dom_aar = ln(nprod_dom_aar)
gen lnprod_exp_aar = ln(nprod_exp_aar)

set scheme s2color

* Figure A.2
graph twoway (kdensity lnprod_dom if cvrnrA!="" & p==0, lwidth(medthick) width(0.15)) (kdensity lnprod_dom if cvrnrT!="", lwidth(medthick) lpattern(longdash) width(0.15)), legend(label(1 "Acquirers") label(2 "Targets") cols(1) position(2) ring(0) size(medlarge)) graphregion(color(white)) ylabel(,labsize(large)) xlabel(,labsize(large)) ytitle("Density", size(large)) xtitle("(log) Number of domestic products", size(large))
graph export "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\figure_A2a.pdf", replace mag(100)
	
graph twoway (kdensity lnprod_exp if cvrnrA!="" & p==0, lwidth(medthick) width(0.15)) (kdensity lnprod_exp if cvrnrT!="", lwidth(medthick) lpattern(longdash) width(0.15)), legend(label(1 "Acquirers") label(2 "Targets") cols(1) position(2) ring(0) size(medlarge)) graphregion(color(white)) ylabel(,labsize(large)) xlabel(,labsize(large)) ytitle("Density", size(large)) xtitle("(log) Number of export products", size(large))
graph export "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\figure_A2b.pdf", replace mag(100)

